//
//  TWMomentsCell.m
//  TWHomework-WeChat
//
//  Created by wzb on 2019/11/14.
//  Copyright © 2019 WZB. All rights reserved.
//

#import "TWMomentsCell.h"
#import "TWTweet.h"
#import "Define.h"
#import "UIImageView+TW.h"
#import "UIView+TW.h"
#import <YYText/YYText.h>
#import "TWUserInfo.h"
#import "TWCommentView.h"

/// 如果只有单张图片时显示的宽高
#define kSingleWH 100
/// 头像左距
#define kAvatarLeft 10

@interface TWMomentsCell ()

// 头像
@property (nonatomic, strong) UIImageView * avatarImageView;
// 时间
@property (nonatomic, strong) UILabel *timeLabel;
// 名称
@property (nonatomic, strong) UILabel *nameLabel;
// 全文
@property (nonatomic, strong) YYLabel *contentLabel;
// 图片
@property (nonatomic, strong) TWImageListView *imageListView;
// 赞和评论视图
@property (nonatomic, strong) TWCommentView *commentView;
// 赞和评论视图背景
@property (nonatomic, strong) UIImageView * bgImageView;

@end

@implementation TWMomentsCell

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self configUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configUI];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configUI];
    }
    return self;
}

- (void)configUI
{
    // 头像
    _avatarImageView = [[UIImageView alloc] init];
    _avatarImageView.backgroundColor = [UIColor grayColor];
    ViewRadius(_avatarImageView, 4, 0, UIColor.clearColor)
    [self.contentView addSubview:_avatarImageView];
    
    // 名字
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont boldSystemFontOfSize:17.0];
    _nameLabel.textColor = kHLTextColor;
    [self.contentView addSubview:_nameLabel];
    
    // 正文视图
    _contentLabel = [[YYLabel alloc] init];
    _contentLabel.font = kTextFont;
    _contentLabel.textColor = [UIColor blackColor];
    _contentLabel.numberOfLines = 0;
    [self.contentView addSubview:_contentLabel];
    
    // 图片区
    _imageListView = [[TWImageListView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:_imageListView];
    
    // 时间视图
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.textColor = RGBColor(110.f, 110.f, 110.f);
    _timeLabel.font = [UIFont systemFontOfSize:13.0f];
    [self.contentView addSubview:_timeLabel];
    
    // 评论视图
    _commentView = [[TWCommentView alloc] init];
    [self.contentView addSubview:_commentView];
}

- (void)setTweet:(TWTweet *)tweet
{
    _tweet = tweet;
    [self.avatarImageView tw_setImage:tweet.sender.avatar];
    self.avatarImageView.frame = CGRectMake(kAvatarLeft, kBlank, kAvatarWidth, kAvatarWidth);
    
    self.nameLabel.text = tweet.sender.username;
    [self.nameLabel sizeToFit];
    self.nameLabel.x = self.avatarImageView.right + kBlank;
    self.nameLabel.y = self.avatarImageView.y;
    
    NSString *content = tweet.content;
    self.contentLabel.text = content;
    CGFloat contentX = self.nameLabel.x;
    CGFloat contentW = k_screen_width - contentX - 15;
    CGFloat contentH = [self sizeWithPreferWidth:contentW font:self.contentLabel.font text:content];
    self.contentLabel.frame = CGRectMake(contentX, self.nameLabel.bottom + 5, contentW, contentH);
    
    self.imageListView.tweet = tweet;
    self.imageListView.x = self.contentLabel.x;
    self.imageListView.y = self.contentLabel.bottom + 10;
    
    NSString *timeStr = @"刚刚";
    if (self.row > 0 && self.row < 6) {
        timeStr = [NSString stringWithFormat:@"%zd分钟前", self.row * 10 + arc4random() % 10];
    }
    if (self.row >= 6) {
        timeStr = [NSString stringWithFormat:@"%zd小时前", self.row - 5];
    }
    self.timeLabel.text = timeStr;
    [self.timeLabel sizeToFit];
    self.timeLabel.x = self.imageListView.x;
    if (self.imageListView.height == 0) {
        self.timeLabel.y = self.contentLabel.bottom + 10;
    } else {
        self.timeLabel.y = self.imageListView.bottom + 10;
    }
    
    if (tweet.comments.count) {
        self.commentView.width = k_screen_width - self.timeLabel.x - 10;
        self.commentView.comments = tweet.comments;
        self.commentView.x = self.timeLabel.x;
        self.commentView.y = self.timeLabel.bottom + 10;
        
        tweet.cellHeight = self.commentView.bottom + 10;
    } else {
        self.commentView.frame = CGRectZero;
        tweet.cellHeight = self.timeLabel.bottom + 10;
    }
}

- (CGFloat)sizeWithPreferWidth:(CGFloat)width font:(UIFont *)font text:(NSString *)text
{
    NSDictionary *attributes = @{ NSFontAttributeName : font };
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:attributes context:nil];
    return ceilf(CGRectGetHeight(rect));
}

@end



@interface TWImageListView ()

@property (nonatomic, strong) NSMutableArray *imageViewsArray;

@end

@implementation TWImageListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 九宫格
        self.imageViewsArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < 9; i++) {
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
            imageView.backgroundColor = k_background_color;
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            [self.imageViewsArray addObject:imageView];
            [self addSubview:imageView];
        }
    }
    return self;
}

- (void)setTweet:(TWTweet *)tweet
{
    // 图片位置绘制
    _tweet = tweet;
    for (UIImageView * imageView in self.imageViewsArray) {
        imageView.hidden = YES;
    }
    // 图片区
    NSInteger count = [tweet.images count];
    if (count == 0) {
        self.size = CGSizeZero;
        return;
    }
    // 添加图片
    UIImageView * imageView = nil;
    NSArray *images = [tweet.images valueForKeyPath:@"url"];
    for (NSInteger i = 0; i < count; i++)
    {
        UIImageView *currentImageView = self.imageViewsArray[i];
        [currentImageView tw_setImage:images[i]];
        NSInteger rowNum = i / 3;
        NSInteger colNum = i % 3;
        if(count == 4) {
            rowNum = i / 2;
            colNum = i % 2;
        }
        CGFloat imageX = colNum * (kImageWidth + kImagePadding);
        CGFloat imageY = rowNum * (kImageWidth + kImagePadding);
        CGRect frame = CGRectMake(imageX, imageY, kImageWidth, kImageWidth);
        
        // 单张图片需计算实际显示size
        if (count == 1) {
            frame = CGRectMake(0, 0, kSingleWH, kSingleWH);
        }
        currentImageView.hidden = NO;
        currentImageView.frame = frame;
        imageView = currentImageView;
    }
    self.width = kTextWidth;
    self.height = imageView.bottom;
}

@end
